const AElfModule = require('../models/aelf')
const PkManageModule = require('../models/private_key_manage')
const HistoryModule = require('../models/history')

exports.form = (req, res) => {
    res.render('cr_unpledge', { title: '取消质押' });
};

exports.unpledge = (req, res) => {
    const data = req.body;
    console.log(data);

    var aelf = new AElfModule();
    var log = new HistoryModule(data.CR_pledgee);
    PkManageModule.get_priKey_by_user(data.CR_pledgee, (user) => {
        aelf.check_chain_state((state) => {
            if (state) {
                aelf.load_wallet(user.priKey, (wallet) => {
                    if(typeof(wallet) !== 'undefined'){
                        aelf.load_contract(true , (param) =>{
                            console.log("contract prepared");
                            aelf.CR_UnPledge(param, data.CR_tid, (ret)=>{
                                log.Log(ret['TransactionId'])
                            });
                        });
                    }
                });
            }
        })
    });
    res.redirect('/services');
}